package com.lincoln.config;

import com.lincoln.entity.client.DbClient;
import com.lincoln.pictureServer.PictureServerApplication;
import com.lincoln.service.TokenClientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 启动服务器前执行
 * Date:2017/8/21.
 * Time:15:36
 */
@Component
@Service
@Order(value = 1)
public class StartRun implements CommandLineRunner, ApplicationListener<ContextRefreshedEvent> {
    private static ApplicationContext applicationContext = null;
    Logger logger = LoggerFactory.getLogger(PictureServerApplication.class);

    @Value("${picture.win-src}")
    String winSrc;

    @Value("${picture.win-other}")
    String winOther;

    @Value("${picture.linux-src}")
    String linuxSrc;

    @Value("${picture.linux-other}")
    String linuxOther;

    @Autowired
    TokenClientService tokenClientService;

    @Override
    public void run(String... args) throws Exception {
        //检查图片路径是否存在,不存在则创建
        boolean isWindows = System.getProperty("os.name").toLowerCase().indexOf("windows") != -1;
        if (isWindows) {
            File file = new File(winSrc);
            if (!file.exists() && !file.isDirectory()) {
                logger.info("启动检测:未发现windows系统的src目录,创建之.");
                file.mkdir();
            }
            file = new File(winOther);
            if (!file.exists() && !file.isDirectory()) {
                logger.info("启动检测:未发现windows系统的other目录,创建之.");
                file.mkdir();
            }
        } else {
            File file = new File(linuxSrc);
            if (!file.exists() && !file.isDirectory()) {
                logger.info("启动检测:未发现linux系统的src目录,创建之.");
                file.mkdir();
            }
            file = new File(linuxOther);
            if (!file.exists() && !file.isDirectory()) {
                logger.info("启动检测:未发现linux系统的other目录,创建之.");
                file.mkdir();
            }
        }
        //检查client数据表,防止有client的resourceIds中没有picture-server
        List<DbClient> clients = tokenClientService.findAll();
        for (DbClient client : clients) {
            if (client.getResourceIds().indexOf("picture-server") == -1) {
                client.setResourceIds((client.getResourceIds() == null ? "" : client.getResourceIds()) + ",picture-server");
                tokenClientService.save(client);
            }
        }
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (applicationContext == null) {
            applicationContext = contextRefreshedEvent.getApplicationContext();
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}
